<template>
  <!-- 表格卡片 -->
  <a-card size="small" :bordered="false" :hoverable="true" :scroll="{ x: 1500 }">
    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block">
        <a-row :gutter="16">
          <a-col :span="6">
            <a-button type="primary" @click="getAddInstantly(4)"> <PlusOutlined />点击添加申请 </a-button></a-col
          >
          <a-col :span="10"> <a-input v-model:value="queryForm.key" placeholder="关键字查询" /></a-col>
          <a-col :span="4">
            <a-button type="primary" @click="getInitDateFind"><SearchOutlined />查询</a-button></a-col
          >
          <a-col :span="2">
            <a-button @click="getRemase"><ReloadOutlined />重置</a-button></a-col
          >
        </a-row>
      </div>
      <div class="smart-table-setting-block">
        <TableOperator v-model="columns" :tableId="TABLE_ID_CONST.BUSINESS.OA.ENTERPRISE" :refresh="getInitDate" />
      </div>
    </a-row>
    <a-table
      size="small"
      :dataSource="tableData"
      :columns="columns"
      :row-key="(record) => record.blfinancprojectid"
      :pagination="false"
      :loading="tableLoading"
      bordered
      @change="handleTableChange"
    >
      <template #bodyCell="{ column, record, text }">
        <template v-if="column.dataIndex === 'action'" title="操作">
          <div class="smart-table-operate">
            <a-space>
              <a-button
                type="primary"
                @click="getAddInstantly(1, record.blfinancprojectid)"
                size="small"
                :disabled="record.blauditstate == 0 || record.blauditstate == -1 ? false : true"
              >
                <FormOutlined />
              </a-button>
              <a-popconfirm
                title="是否要删除此条申报?"
                ok-text="确定"
                cancel-text="取消"
                @confirm="confirm(record.blfinancprojectid)"
                :disabled="record.blauditstate == 0 || record.blauditstate == -1 ? false : true"
              >
                <a-button size="small" :disabled="record.blauditstate == 0 || record.blauditstate == -1 ? false : true"><DeleteOutlined /> </a-button>
              </a-popconfirm>
            </a-space>
          </div>
        </template>
        <template v-if="column.dataIndex === 'blprojectname'" title="项目名称">
          <div class="smart-table-operate">
            <span style="color: #66a5fd" @click="getAddInstantly(2, record.blfinancprojectid)"> {{ record.blprojectname }}</span>
          </div>
        </template>
        <template v-if="column.dataIndex === 'declaration'" title="申报流程">
          <div class="smart-table-operate">
            <span style="color: #66a5fd" @click="getAddInstantly(3, record.blfinancprojectid, record.blauditstate)"> 点击查看审核流程</span>
            <span v-if="record.blauditstate == 5" style="margin-left: 10px; color: #66a5fd" @click="getAddInstantly(5, record.blprojectnum)"
              >捐赠人信息</span
            >
          </div>
        </template>
        <template v-if="column.dataIndex === 'blauditstate'" title="申请状态">
          <div class="smart-table-operate">
            <span v-if="record.blauditstate == 0">资料已填写等待审核</span>
            <span v-if="record.blauditstate == 1">基金会审核</span>
            <span v-if="record.blauditstate == -1">基金会审核未通过</span>
            <span v-if="record.blauditstate == 2">基金会审核通过,等待递交纸质件</span>
            <span v-if="record.blauditstate == 3 || record.blauditstate == 4">收到纸质件等待基金确认</span>
            <span v-if="record.blauditstate == 5">申报成功</span>
          </div>
        </template>
      </template>
    </a-table>
    <!-- 分页-->
    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :pageSizeOptions="PAGE_SIZE_OPTIONS"
        :defaultPageSize="queryForm.limit"
        v-model:current="queryForm.page"
        v-model:pageSize="queryForm.limit"
        :total="total"
        @change="getInitDate"
        @showSizeChange="getInitDate"
        :show-total="(total) => `共${total}条`"
      />
    </div>
  </a-card>
</template>
<script setup>
  import { reactive, ref, onMounted, defineEmits } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { Funding } from '/@/api/funding/funding-api';
  import { PAGE_SIZE, PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
  import TableOperator from '/@/components/support/table-operator/index.vue';
  import { TABLE_ID_CONST } from '/@/constants/support/table-id-const';
  import { smartSentry } from '/@/lib/smart-sentry';
  //--------------------------- 表格 列 ---------------------------
  const columns = ref([
    {
      title: '项目名称',
      dataIndex: 'blprojectname',
      align: 'center',
      width: 150,
    },
    {
      title: '项目申请时间',
      dataIndex: 'bldate',
      align: 'center',
      sorter: (a, b) => {
        let aTimeString = a.bldate;
        let bTimeString = b.bldate;
        aTimeString = aTimeString.replace(/-/g, '/');
        bTimeString = bTimeString.replace(/-/g, '/');
        let aTime = new Date(aTimeString).getTime();
        let bTime = new Date(bTimeString).getTime();
        return bTime - aTime;
      },
    },
    {
      title: '申请单位',
      dataIndex: 'blsubject',
      align: 'center',
    },
    {
      title: '联系人',
      dataIndex: 'bllinker',
      align: 'center',
    },
    {
      title: '联系方式',
      dataIndex: 'bllinktel',
      align: 'center',
    },
    {
      title: '申请状态',
      dataIndex: 'blauditstate',
      align: 'center',
    },
    {
      title: '申请流程',
      dataIndex: 'declaration',
      align: 'center',
    },
    {
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      align: 'center',
      width: 100,
    },
  ]);
  const queryFormState = {
    page: 1,
    limit: PAGE_SIZE,
  };
  const queryForm = reactive({ ...queryFormState });
  const tableLoading = ref(false);
  const tableData = ref([]);
  const total = ref(0);
  // 表格初始化
  function getInitDateFind() {
    message.success('查询成功');
    getInitDate();
  }
  //重置
  function getRemase() {
    message.success('重置成功');
    queryForm.key = '';
    getInitDate();
  }
  async function getInitDate() {
    try {
      let res = await Funding.FundingInit(queryForm);
      console.log(res, '初始化');
      tableData.value = res.page.list;
      total.value = res.page.totalCount;
    } catch (error) {
      smartSentry.captureError(error);
    }
  }
  //删除接口
  const confirm = async (recordId) => {
    let blfinancprojectid = [];
    blfinancprojectid.push(recordId);
    try {
      let res = await Funding.deleteFunding(blfinancprojectid);
      const totalPage = Math.ceil((total.value - 1) / PAGE_SIZE);
      const currentPage = queryForm.page > totalPage ? totalPage : queryForm.page;
      queryForm.page = currentPage < 1 ? 1 : currentPage;
      message.success('删除成功');
      getInitDate();
    } catch (error) {
      smartSentry.captureError(error);
    }
  };

  //返回立项申请表格页面
  const emit = defineEmits();
  const getAddInstantly = (e, id, status) => {
    console.log(e, 'jinru');
    let obj = {
      type: e,
      id: id,
      status,
    };
    emit('getBlack', obj);
  };

  onMounted(() => {
    getInitDate();
  });
</script>
